package com.good.serviceImp;

import javax.annotation.Resource;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.good.dao.AdminDao;
import com.good.entity.Admin;
import com.good.service.AdminService;
@Service("adminServiceImpl")
@Transactional
public class AdminServiceImpl extends BaseServiceImpl<Admin, Long> implements
		AdminService {
	@Resource(name = "adminDaoImpl")
	private AdminDao adminDao;

	@Resource(name = "adminDaoImpl")
	public void setBaseDao(AdminDao adminDao) {
		super.setBaseDao(adminDao);
	}

	@Override
	/**
	 * allEntries是boolean类型，表示是否需要清除缓存中的所有元素
	 * 
	 */
	@CacheEvict(value ="adminTest", allEntries = true)
	@Transactional(readOnly = true)
	/**
	 * 如果缓存中有数据就从缓存中拿,如果没有就数据库里面拿并且缓存到硬盘
	 * value:xml里面配置的key
	 * key:spel表达式
	 * condition满足条件才缓存
	 */
	@Cacheable(value="adminTest",key="#account",condition="#account%2==0")
	/**
	 * 不论缓存中有没有数据都从数据库里面拿,然后缓存起来
	 */
	//@CachePut(value="adminTest")
	public Admin getByAccount(String account) {
		String hqlString="from Admin admin where admin.account=?";
		return adminDao.findByHQL(hqlString, account);
	}

	@Override
	@Transactional(readOnly=true)
	public Admin callProcedure() {
		return adminDao.callProcedure();
	}
	
}
